package com.jews.framework.json.exclusion;

import com.jews.framework.json.annotation.JsonExcludable;
import com.jn.easyjson.core.exclusion.Exclusion;
import com.jn.langx.util.reflect.FieldAttributes;
import com.jn.langx.util.reflect.MethodAttributes;

/**
 * @author Bran Wang
 */
public class JsonExcludableAnnotationExclusion implements Exclusion {

    @Override
    public boolean shouldSkipMethod(MethodAttributes m, boolean serialize) {
        JsonExcludable jsonExcludable = m.getAnnotation(JsonExcludable.class);
        if (jsonExcludable == null) {
            return false;
        }
        return (serialize && jsonExcludable.write()) || (!serialize && jsonExcludable.read());
    }

    @Override
    public boolean shouldSkipField(FieldAttributes f, boolean serialize) {
        JsonExcludable jsonExcludable = f.getAnnotation(JsonExcludable.class);
        if (jsonExcludable == null) {
            return false;
        }
        return (serialize && jsonExcludable.write()) || (!serialize && jsonExcludable.read());
    }

    @Override
    public boolean shouldSkipClass(Class<?> clazz, boolean serialize) {
        return false;
    }
}